from kafka import KafkaConsumer, KafkaProducer

HOST = 'localhost'
PORT = '9092'
CHANNEL = 'channel01'


class KafkaHelper(object):

    def __init__(self, host=HOST, port=PORT, channel=CHANNEL):
        self.host = host
        self.port = port
        self.channel = channel

    def subscribe(self):
        consumer = KafkaConsumer(self.channel, group_id='group2',
                                 bootstrap_servers='{}:{}'.format(self.host, self.port), api_version=(0, 10))
        for message in consumer:
            print(message)

    def publish(self, msg):
        producer = KafkaProducer(bootstrap_servers='{}:{}'.format(self.host, self.port), api_version=(0, 10))
        producer.send(self.channel, str.encode(msg))
